package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.charitable.CharitableSpecialEvent;
import com.yanqu.road.entity.config.charitable.CharitableWordConfig;
import com.yanqu.road.entity.config.charitable.CharitableWorkConsumeConfig;
import com.yanqu.road.entity.enums.eCharitableSpecialEventRewardType;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wpj
 * @Date 2020/8/29
 */
public class CharitableConfigDaoImpl extends TempDao {
    public static String getCharitableWordConfigSql = "select * from t_p_charitable_work";
    public Map<Integer, CharitableWordConfig> getCharitableWordConfig() throws Exception {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, CharitableWordConfig> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getCharitableWordConfigSql);
            rs = call.executeQuery();
            while (rs.next()) {
                CharitableWordConfig charitableWordConfig = new CharitableWordConfig();
                charitableWordConfig.setId(rs.getInt("id"));
                charitableWordConfig.setType(rs.getInt("type"));
                charitableWordConfig.setSkills(StringUtils.stringToIntegerList(rs.getString("skills"), "\\|"));
                charitableWordConfig.setGlamourAddList(StringUtils.stringToIntegerList(rs.getString("glamourAddList"), "\\|"));
                charitableWordConfig.setRewardBoxList(StringUtils.stringToIntegerList(rs.getString("rewardBoxList"), "\\|"));
                List<String> list = StringUtils.stringToStringList(rs.getString("specialGuideId"), "\\|");
                List<CharitableSpecialEvent> charitableSpecialEventList = new ArrayList<>();
                for (String s : list) {
                    CharitableSpecialEvent charitableSpecialEvent = new CharitableSpecialEvent();
                    List<Integer> paramList = StringUtils.stringToIntegerList(s, ";");
                    charitableSpecialEvent.setGuideId(paramList.get(0));
                    charitableSpecialEvent.setRewardType(eCharitableSpecialEventRewardType.forValue(paramList.get(1)));
                    charitableSpecialEvent.setRewardNum(paramList.get(2));
                    charitableSpecialEvent.setWeight(paramList.get(3));
                    charitableSpecialEventList.add(charitableSpecialEvent);
                }
                charitableWordConfig.setSpecialGuideList(charitableSpecialEventList);

                map.put(charitableWordConfig.getType(), charitableWordConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String getCharitableWorkConsumeConfigSql = "select * from t_p_charitable_work_consume";
    public Map<Integer, CharitableWorkConsumeConfig> getCharitableWorkConsumeConfig() throws Exception {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, CharitableWorkConsumeConfig> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getCharitableWorkConsumeConfigSql);
            rs = call.executeQuery();
            while (rs.next()) {
                CharitableWorkConsumeConfig charitableWorkConsumeConfig = new CharitableWorkConsumeConfig();
                charitableWorkConsumeConfig.setWorkNum(rs.getInt("workNum"));
                charitableWorkConsumeConfig.setConsume(rs.getString("consume"));
                map.put(charitableWorkConsumeConfig.getWorkNum(), charitableWorkConsumeConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

}
